<template>
  <div>
    <div class="search">
      <input type="text" v-model="keyword" placeholder="输入城市名字或拼音" />
    </div>

    <transition
      enter-active-class="animate__animated animate__fadeIn"
      leave-active-class="animate__animated animate__fadeOut"
    >
      <!-- keyword有值时显示 -->
      <div v-show="keyword" ref="searchResult" class="search-result">
        <ul>
          <li
            v-for="item in list"
            :key="item.id"
            @click="handleSelectCity(item.name)"
            class="search-item border-bottom"
          >
            {{ item.name }}
          </li>
          <li
            v-show="isShow"
            class="search-item border-bottom search-item-null"
          >
            没有找到匹配数据！
          </li>
        </ul>
      </div>
    </transition>
  </div>
</template>

<script>
import BScroll from "better-scroll";
import {mapActions} from "vuex"
import "animate.css";
export default {
  name: "Search",
  props: {
    cities: {
      type: Object
    }
  },
  data() {
    return {
      keyword: "", // 搜索关键词
      list: [], // 查询结果
      timer: null
    };
  },
  watch: {
    // 根据关键词显示搜索结果
    keyword() {
      if (!this.timer) {
        clearTimeout(this.timer);
      }
      this.timer = setTimeout(() => {
        const result = [];
        for (let i in this.cities) {
          this.cities[i].forEach(city => {
            // 关键词不为空且拼音或者城市包含有关键词
            if (
              this.keyword != "" &&
              (city.spell.includes(this.keyword) ||
                city.name.includes(this.keyword))
            ) {
              result.push(city);
            }
          });
        }
        this.list = result;
      }, 100);
    }
  },

  methods: {
    ...mapActions(['selectCity']),
    // 选择城市并跳转回首页
    handleSelectCity(city) {
      // this.$store.dispatch("selectCity", city);
      // 修改当前城市
      this.selectCity(city);
      // 跳转回首页
      this.$router.push({
        name:'Home'
      })
    }
  },

  mounted() {
    // 搜索列表可以滚动
    this.scroll = new BScroll(this.$refs.searchResult, {
      click: true,
      tap: true
    });
  },
  computed: {
    // 是否显示“没有匹配数据”提示信息
    isShow() {
      return !this.list.length;
    }
  }
};
</script>

<style lang="less" scoped>
@import url("~@/assets/styles/varibles.less");
.search {
  height: 0.6rem;
  padding: 0 0.2rem;
  background-color: @BgColor;
  input {
    width: 100%;
    height: 0.5rem;
    padding: 0 0.2rem;
    box-sizing: border-box;
    border-radius: 0.08rem;
    text-align: center;
    color: #666;
  }
}
.search-result {
  position: absolute;
  top: 1.46rem;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  background-color: #eee;
  z-index: 1;
}

.search-item {
  height: 0.4rem;
  padding: 0.2rem;
  line-height: 0.4rem;
  color: #666;
  background-color: #fff;
  &-null {
    text-align: center;
  }
}
</style>
